// pages/index/special/special.js
import {list as designerList} from '../../../api/designer'
import { find } from '../../../api/wxPage'
const util = require('../../../utils/util')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    length:'',
    tageId:'',
    isSwiperShow:true,
    componentsList:[],
    hiddenNav: true,
    backgroundColor:"",
    id:'',
    shareDescr:"",
    shareImage:"",
    title:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.info("options--",options)
    if(options.title){
      this.setData({
        title: options.title,
      });
    }
    this.setData({
      statusBarHeight: app.globalData.statusBarHeight,
    });
    this.data.id = options.id
    this.loadPage(options.id)
    this.getPageInfo(options.id)
    app.userInfoReadyCallback = res => {
      if (app.globalData.token != null) {
        util.forwarding(options,app.globalData.token)
      }
    }
    app.event.emit("onCartChange");
  },
  onPageScroll(e){
    if(e.scrollTop>217){
      this.data.isSwiperShow=false
 }else{
   this.data.isSwiperShow=true
 }
 this.setData({
  isSwiperShow:this.data.isSwiperShow
 })
  },
  // 监听滚动事件
  bindscroll(e) {
  
    let _this = this;
    let query = wx.createSelectorQuery()
    query.select('#anchor1').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec(function (res) {
      console.log(res)
      if (res[0].top > -200 && _this.data.hiddenNav == false) {
        _this.setData({
          hiddenNav: true
        })
      } else if (res[0].top < -190 && _this.data.hiddenNav == true) {
        _this.setData({
          hiddenNav: true
        })
      }
    })
   
  },  
  onPullDownRefresh() {
    var _this=this
    wx.showNavigationBarLoading()
    setTimeout(() => {
      wx.hideNavigationBarLoading()
      _this.data.componentsList.forEach(item=>{
        if (item.name==='products') {
          _this.selectComponent('#products').downRefresh();
        }else if (item.name==='productRow') {
          _this.selectComponent('#productRow').downRefresh();
        }else if (item.name==='groupProduct') {
          _this.selectComponent('#groupProduct').downRefresh();
        }else if (item.name==='groupProductRow') {
          _this.selectComponent('#groupProductRow').downRefresh();
        }else if (item.name==='bargainProduct') {
          _this.selectComponent('#bargainProduct').downRefresh();
        }else if (item.name==='bargainProductRow') {
          _this.selectComponent('#bargainProductRow').downRefresh();
        }else if (item.name==='conciseProductRow') {
          _this.selectComponent('#conciseProductRow').downRefresh();
        }else if (item.name==='conciseProduct') {
          _this.selectComponent('#conciseProduct').downRefresh();
        }
      })
      wx.stopPullDownRefresh()
    }, 1000);
  },

  onReachBottom() {
    if(this.data.componentsList.length>0){
      if (this.data.componentsList[this.data.length - 1].name == 'products') {
        this.selectComponent('#products').onReachBottom();
       }else if(this.data.componentsList[this.data.length - 1].name == 'productRow'){
         this.selectComponent('#productRow').onReachBottom();
       }else if(this.data.componentsList[this.data.length - 1].name == 'groupProduct'){
        this.selectComponent('#groupProduct').onReachBottom();
      }else if(this.data.componentsList[this.data.length - 1].name == 'groupProductRow'){
        this.selectComponent('#groupProductRow').onReachBottom();
      }else if(this.data.componentsList[this.data.length - 1].name == 'bargainProduct'){
        this.selectComponent('#bargainProduct').onReachBottom();
      }else if(this.data.componentsList[this.data.length - 1].name == 'bargainProductRow'){
        this.selectComponent('#bargainProductRow').onReachBottom();
      }else if(this.data.componentsList[this.data.length - 1].name == 'conciseProductRow'){
        this.selectComponent('#conciseProductRow').onReachBottom();
      }else if(this.data.componentsList[this.data.length - 1].name == 'conciseProduct'){
        this.selectComponent('#conciseProduct').onReachBottom();
      }else if(this.data.componentsList[this.data.length - 1].name == 'recommendShop'){
        this.selectComponent('#recommendShop').onReachBottom();
      }
    }
  },

  getPageInfo(id){
    find(id).then(res=>{
      console.info(res)
      if(res.data.wxPage.hasOwnProperty('backgroundColor') && !util.isNull(res.data.wxPage.backgroundColor)){
        this.setData({
          backgroundColor:res.data.wxPage.backgroundColor,
        })
      } 
      if(res.data.wxPage.hasOwnProperty('shareImage') && !util.isNull(res.data.wxPage.shareImage)){
        this.data.shareImage = res.data.wxPage.shareImage
        this.setData({
          shareImage:res.data.wxPage.shareImage
        })
      } 
      if(res.data.wxPage.hasOwnProperty('shareDescr') && !util.isNull(res.data.wxPage.shareDescr)){
        this.data.shareDescr = res.data.wxPage.shareDescr
        this.setData({
          shareDescr:res.data.wxPage.shareDescr
        })
      } 
  })
},

  loadPage(e){
    let _this = this
    designerList({
      mchId: app.globalData.mchId,
      pageId:e,
    }).then(res=>{
      this.data.componentsList=res.data
      if(this.data.componentsList.length > 0){
        _this.data.componentsList.map(item=>{
          if(item.name == "commonHeader"){
            item.pageName = _this.data.title
          }
        })
      }
      console.log("this.data.componentsList-----",this.data.componentsList)
      this.data.length=res.data.length
      this.setData({
        componentsList:this.data.componentsList
      })
   
    })
  },
  backClick() {
    console.log('111')
    wx.navigateBack({
      delta: 1,
      fail: res => {
        // wx.showToast({
        //       icon: "none",
        //   title: res.content,
        // })
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
    })
  },
  onShareAppMessage: function () {
    let shareImage = util.isNull(this.data.shareImage)?"":util.thumbnail(this.data.shareImage,500,400)  
    let shareDescr = util.isNull(this.data.shareDescr)?app.globalData.mchName:this.data.shareDescr
    return {
      title: shareDescr,
      desc: '分享页面的内容',
      imageUrl:shareImage,
      path: '/pages/index/wxPage/wxPage?id=' + this.data.id + '&xuid=' + app.globalData.userInfo.id
    }
  },
  onShareTimeline: function () {
    let shareImage = util.isNull(this.data.shareImage)?"":util.thumbnail(this.data.shareImage,500,400)  
    let shareDescr = util.isNull(this.data.shareDescr)?app.globalData.mchName:this.data.shareDescr
    return {
      title: shareDescr,
      desc: '分享页面的内容',
      path: '/pages/index/wxPage/wxPage',
      imageUrl: shareImage,
      query: 'id=' + this.data.id + '&xuid=' + app.globalData.userInfo.id
    }
  }
})